using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Universum.Model.Services.Indexing;
using Universum.Infrastructure.Configuration;
using Universum.Model.Domain.Blogs;
using Universum.Model.Domain.QuestionIT;
using Universum.Model.Domain.Classes;
using Universum.Model.Domain;

namespace Universum.Controllers
{
	[Authorize]
	public class SearchController : UniversumController
	{
		const string DATA = "DATA";
		const string SEARCH_FOR = "search-for";

		IBlogArticleIndexingService _BlogArticleIndexingService;
		ITeacherArticleIndexingService _TeacherArticleIndexingService;
		IQuestionIndexingService _QuestionIndexingService;
		public SearchController(
			System.Security.Principal.IPrincipal principal,
			IBlogArticleIndexingService blogArticleIndexingService,
			ITeacherArticleIndexingService teacherArticleIndexingService,
			IQuestionIndexingService questionIndexingService
			)
			: base(principal)
		{
			_BlogArticleIndexingService = blogArticleIndexingService;
			_TeacherArticleIndexingService = teacherArticleIndexingService;
			_QuestionIndexingService = questionIndexingService;
		}
		[AcceptVerbs( HttpVerbs.Get)]
		public ActionResult Index()
		{
			ViewData.Model = new List<Article>();
			return View();
		}
		//
		// Post: /Search/
		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Index(FormCollection values)
		{
			var place = (string)values[Universum.Infrastructure.Configuration.ConstantIdentifiers.SEARCH_IN_PLACE];
			var search_for = values[SEARCH_FOR].ToString();

			// I'm redirecting to a new page to avoid 
			// the browser's requests on history.back()
			// by using a redirect, and saving the data
			// in TempData
			if (search_for.IsNullOrEmpty().IsFalse())
			{
				switch (place)
				{
					case ConstantIdentifiers.SEARCH_IN_QUESTIONS:
						List<Question> questions =
							((IIndexingServiceBase<Universum.Model.Domain.QuestionIT.Question>)_QuestionIndexingService).Find(
							search_for,
							0,
							ConstantIdentifiers.RESULTS_PER_PAGE
							);
						//ViewData.Model = questions;
						TempData[DATA] = questions;
						return RedirectToAction<SearchController>(it => it.Questions());

					case ConstantIdentifiers.SEARCH_IN_BLOGS:
						{
							var articles =
					_BlogArticleIndexingService.Find(
						search_for,
						0,
						ConstantIdentifiers.RESULTS_PER_PAGE);
							// select only the types that are required
							//ViewData.Model = articles ;
							TempData[DATA] = articles;
							return RedirectToAction<SearchController>(it => it.Blogs());
						}

					case ConstantIdentifiers.SEARCH_IN_TEACHER_ARTICLES:

						var teacherArticles =
							_TeacherArticleIndexingService.Find(
							search_for,
							0,
							ConstantIdentifiers.RESULTS_PER_PAGE
							);
						//ViewData.Model = teacherArticles;
						TempData[DATA] = teacherArticles;
						return RedirectToAction<SearchController>(it => it.Articles());

				}
			}
			ViewData.Model = new List<Article>();
			return View();
		}
		
		public ActionResult Articles()
		{
			ViewData.Model = TempData[DATA]?? new List<PageArticle>();
			return View();
		}
		public ActionResult Questions()
		{
			ViewData.Model = TempData[DATA] ?? new List<Question>();
			return View();
		}
		public ActionResult Blogs()
		{
			ViewData.Model = TempData[DATA] ?? new List<BlogArticle>();
			return View();
		}

	}
}
